package com.simplecontact.providers;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import com.simplecontact.classes.ContactDb;
import com.simplecontact.classes.GroupContact;
import com.simplecontact.databases.ContactDbOpenHelper;

public class GroupContactProvider extends ContentProvider {

	private ContactDbOpenHelper dbsur;
	private static final UriMatcher sUriMatcher;
	private static HashMap<String, String> sDicProjectionMap;
	
	@Override
	public int delete(Uri uri, String where, String[] whereArgs) {
		SQLiteDatabase db = dbsur.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
        case 1:
            count = db.delete(GroupContact.GROUP_CONTACT_TABLE_NAME, where, whereArgs);
            break;        
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
	}

	@Override
	public String getType(Uri uri) {
		switch (sUriMatcher.match(uri)) {
        case 1:        
            return GroupContact.GroupContactColumns.CONTENT_TYPE;
        case 2:
            return GroupContact.GroupContactColumns.CONTENT_ITEM_TYPE;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
	}

	@Override
	public Uri insert(Uri uri, ContentValues initialValues) {
		if (sUriMatcher.match(uri) != 1) {
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        ContentValues values;
        if (initialValues != null) {
            values = new ContentValues(initialValues);
        } else {
            values = new ContentValues();
        }       

        if (values.containsKey(GroupContact.GroupContactColumns.CONTACTID) == false) {            
            values.put(GroupContact.GroupContactColumns.CONTACTID, 0);
        }

        if (values.containsKey(GroupContact.GroupContactColumns.GROUPID) == false) {            
            values.put(GroupContact.GroupContactColumns.GROUPID, 0);
        }
        
        SQLiteDatabase db = dbsur.getWritableDatabase();
        long rowId = db.insert(GroupContact.GROUP_CONTACT_TABLE_NAME, null, values);
        if (rowId > 0) {
            Uri surUri = ContentUris.withAppendedId(GroupContact.GroupContactColumns.CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(surUri, null);
            return surUri;
        }
        throw new SQLException("Failed to insert row into " + uri);
	}

	@Override
	public boolean onCreate() {
		dbsur = new ContactDbOpenHelper(getContext(), ContactDb.CONTACT_DATABASE_NAME, null, ContactDb.DATABASE_VERSION);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(GroupContact.GROUP_CONTACT_TABLE_NAME);

        switch (sUriMatcher.match(uri)) {
        case 1:
            qb.setProjectionMap(sDicProjectionMap);
            break;        
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        // Get the database and run the query
        SQLiteDatabase db = dbsur.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, null);

        // Tell the cursor what uri to watch, so it knows when its source data changes
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
		SQLiteDatabase db = dbsur.getWritableDatabase();
        int count;
        switch (sUriMatcher.match(uri)) {
        case 1:
            count = db.update(GroupContact.GROUP_CONTACT_TABLE_NAME, values, where, whereArgs);
            break;        
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
	}
	static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(GroupContact.AUTHORITY, "groupcontact", 1);
        sUriMatcher.addURI(GroupContact.AUTHORITY, "groupcontact/#", 2);        

        sDicProjectionMap = new HashMap<String, String>();
        sDicProjectionMap.put(GroupContact.GroupContactColumns.CONTACTID, GroupContact.GroupContactColumns.CONTACTID);
        sDicProjectionMap.put(GroupContact.GroupContactColumns.GROUPID, GroupContact.GroupContactColumns.GROUPID);
    }
}
